<template>
  <div class="login">
    <div class="login-form_bg">
      <h3 class="title">用户登录</h3>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <!-- <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <img src="../assets/images/user.png" alt=""  >
        </el-input> -->
          <el-input v-model="loginForm.username" placeholder="请输入账号">
            <template #prefix>
              <div class="prefixbg">
                <img
                  src="@/assets/images/user.png"
                  alt=""
                  class="prefix-icon"
                />
              </div>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">

          <el-input
            v-model="loginForm.password"
            type="password"
            auto-complete="off"
            placeholder="请输入密码"
            @keyup.enter.native="handleLogin"
          >
            <template #prefix>
              <div class="prefixbg">
                <img
                  src="@/assets/images/password.png"
                  alt=""
                  class="prefix-icon"
                />
              </div>
            </template>
          </el-input>
        </el-form-item>
        <el-checkbox
          v-model="loginForm.rememberMe"
          style="margin: 0px 0px 15px 25px;color: #a9ceff;"
          >记住密码</el-checkbox
        >
        <el-form-item>
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            class="loginButton"
            @click.native.prevent="handleLogin"
          >
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div style="float: right" v-if="register">
            <router-link class="link-type" :to="'/register'"
              >立即注册</router-link
            >
          </div>
        </el-form-item>
      </el-form>
    </div>

    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span>Copyright © 2018-2025 ruoyi.vip All Rights Reserved.</span>
    </div> -->
  </div>
</template>

<script>
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";

export default {
  name: "Login",
  data() {
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
        ],
        // code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
        console.log( this.redirect)
      },
      immediate: true,
    },
  },
  created() {
    // this.getCode()
    this.getCookie();
  },
  methods: {
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get("rememberMe");
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : password,
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), {
              expires: 30,
            });
            Cookies.set("rememberMe", this.loginForm.rememberMe, {
              expires: 30,
            });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove("rememberMe");
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({ path: '/index' }).catch(() => {});
               localStorage.setItem("actived", 0);
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                // this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.login {
   display: flex;
  justify-content: flex-end; /* 将内容靠右对齐 */
  align-items: center;
  height: 100vh; /* 使用视口高度确保全屏 */
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
  padding-right: 170px;
}

.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #fff;
  font-size: 30px;
  font-family: Adobe Heiti Std R;
}
.login-form_bg {
   width: 548px;
  display: flex;
  flex-direction: column;
}
.login-form {
  border-radius: 10px;
  background: rgba(0, 42, 110, 0.63); /* 背景透明，文字不透明 */
  width: 548px;
  padding: 25px 25px 5px 25px;
  border: 1px solid #1a5ecc;

  z-index: 1;
  .el-form-item {
    width: 450px;
    margin: 30px auto;
  }
}
::v-deep .el-input {
  width: 450px !important;
  height: 60px !important;
  margin: auto;
  background: transparent !important; // 修改为透明背景
}
::v-deep .el-input__inner {
  width: 405px !important;
  background: transparent !important;
  color: #fff !important;
  height: 60px !important;
  line-height: 60px !important;
  margin-left: 50px !important;
  border: 1px solid #0c3e9b !important; // 确保边框颜色生效
}
::v-deep input {
  background: transparent;
  color: #fff;
  font-size: 16px;
  &::placeholder {
    color: #a9ceff;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
.loginButton {
  border-radius: 10px;
  background: #0c3e9b;
  font-size: 28px;
  font-family: Microsoft Yahei;
  font-weight: bold;
  color: #bfd9fc;
  width: 450px;
  height: 75px;
  margin: auto;
}
.el-checkbox__label {
  font-family: Microsoft Yahei;
  font-size: 16px;
  color: #a9ceff;
}

::v-deep .el-input__wrapper {
  background: #025796 !important;
  box-shadow: none !important;
  border-radius: 5px !important;
  padding: 0 15px !important;

  border: 1px solid #0c5e5d !important; // 确保边框颜色生效
}

// 前缀区域样式
::v-deep .el-input__prefix {
  left: 0px !important;
  display: flex;
  align-items: center;
  background-color: #0c3e9b !important;
  height: 100%;
  .prefixbg {
    background-color: #0c3e9b !important;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 5px 0 0 5px;
    margin-left: 3px;
    width: 60px;
    // margin-top: 1px;
    height: 58px;
    .prefix-icon {
      display: flex;
      align-items: center;
      width: 26px;
      height: 27px;
    }
  }
}
</style>
